package com.haizhi.task;

import com.haizhi.exit.StopEnable;
import com.haizhi.thread.DispatchThread;
import com.haizhi.thread.ReportThread;
import com.haizhi.thread.TaskThread;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: youfeng
 * Date: 2018-06-25
 * Time: 18:01
 */
public class Scheduler implements StopEnable {

    private static final Logger logger = LogManager.getLogger(Scheduler.class);

    // 线程任务列表
    private List<TaskThread> taskList = new ArrayList<>();

    public Scheduler() throws Exception {
        taskList.add(new DispatchThread());
        taskList.add(new ReportThread());
    }

    //启动调度
    public void start() {
        logger.info("开始启动所有线程...");
        for (Thread thread : taskList) {
            thread.start();
        }

        logger.info("所有线程启动完成...");
        for (Thread thread : taskList) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                logger.error("线程阻塞异常:", e);
            }
        }
    }

    @Override
    public void shutdown() {
        for (StopEnable stopEnable : taskList) {
            stopEnable.shutdown();
        }
    }
}
